<%@page import="org.datanucleus.query.evaluator.memory.StringLengthMethodEvaluator"%>
<%@ page import="agentFree2.*,java.net.*,java.io.*,java.nio.charset.*,java.nio.*,java.util.Date,java.util.logging.Logger,java.util.regex.Pattern,java.util.regex.Matcher,javax.jdo.PersistenceManager,javax.servlet.http.*" %>
<%
	//Stage1. Parsing Url (http://irr.by/realestate/rooms/rent/1236499/) Source and return adv content if it is not agent adv.
	
	Pattern phoneNumber = Pattern.compile("</div>[0-9,-,(,)]{7,15}</");
	
	String tempUrl = request.getParameter("URL");
	URL u = new URL(tempUrl);
	String s;
	Boolean isAgent = false;
	s = ReaderFactory.readUrlData(u,"ISO-8859-1");
	//out.println(s);
	Matcher phoneMatcher = phoneNumber.matcher(s);
    //OPen connection to datastore.

	
	while (phoneMatcher.find()) {
		String phone = phoneMatcher.group();	
		System.out.println("phone"+phone);		
		phone = phone.replaceAll("[-, ,(,),-,a-z,A-Z,/,<,>]*","");
		phone = phone.replaceFirst("^\\+375|^80","");
		System.out.println("phone"+phone);
		Long phoneNumberParsed = Long.parseLong(phone);
		if (Utils.isRealAgent(new Long(phoneNumberParsed)))isAgent = true;
	}

	if (!isAgent){
	//Adv text
		try{
			String output;
			//out.println(s);
			//out.println(Utils.findFirstMatcher(s,"<div class=\"h3 w-title black\" style=\"padding-bottom: 11px;\">.{0,300}</div>"));
			output = Utils.findFirstMatcher(s,"<div style=\"padding-bottom: 11px;\" class=\"h3 w-title black\">.*<p class=\"da-add\">")
			+"|"+(Utils.findFirstMatcher(s,"([0-9]{2}.){2}[0-9]{2} [0-9]{2}:[0-9]{2}"))
			+"|"+(Utils.findFirstMatcher(s,"<div class=\"cost-big\">.{0,200}</span>"));
			out.println(output);
		}catch(Exception e){
			out.println(e.getMessage());
		}
		
	}
%>